package cn.wolfcode.luowowo2.util;

import cn.wolfcode.luowowo2.common.util.DisableExeption;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Albert on 2019/7/5.
 */
@ControllerAdvice
public class CommonExceptionHandler {
    @ExceptionHandler(DisableExeption.class)
    public void disableExp(DisableExeption e, HttpServletResponse response) throws IOException {
        e.printStackTrace();
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(new AjaxResult(e.getMessage())));
    }

    @ExceptionHandler(RuntimeException.class)
    public void runTimeExp(Exception e,HttpServletResponse response) throws IOException {
        e.printStackTrace();
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(new AjaxResult("系统错误,联系管理员")));
    }
}
